<?php
class lib_site_device extends lib_base{
	public $table_name = "qc_site_device";
    public $pk = "id";

    public function add($data)
    {
        $data['status'] = 1;
        $data['add_time'] = date('Y-m-d H:i:s');
        if($data['setting']){
            $data['setting'] = serialize($data['setting']);
        }
        return $this->create($data);
    }

    public function get_device($id)
    {
        $info = $this->findById($id);
        $info['setting'] = unserialize($info['setting']);
        return $info;
    }

    public function get_detail($id)
    {
        $info = $this->findById($id);
        $info['setting'] = unserialize($info['setting']);

        if($info['photo']){
            $photos_arr = explode(',', $info['photo']);
            foreach($photos_arr as $v){
                $info['photos'][] = D('upload_file')->get_image_url($v);
            }
        }

        $cond = "status=1 and device_id={$info['id']}";
        $logs = D('company_order_log')->get_list($cond);
        if($logs['list']){
            foreach ($logs['list'] as $key => $val) {
                if($val['intro']){
                    $val['intro'] = unserialize($val['intro']);
                    if($val['intro']['photo']){
                        $photos_arr = explode(',', $val['intro']['photo']);
                        foreach($photos_arr as $v){
                            $val['intro']['photos'][] = D('upload_file')->get_image_url($v);
                        }
                    }
                    
                }
                $list[] = $val;
            }
            
            $info['logs'] = $list;
            $info['logs_maxid'] = min(array_keys($list));
        }
        return $info;
    }



    //二维码
	public function QrCode($url,$info,$company){
		header("Content-Type: text/html;charset=utf-8");
		// $id = arg('id');
		// $this->info = $this->findById($id);
		// $this->zone = $this->zoneObj->findById($this->info['zone_id']);

		require(APP_DIR.'/protected/include/phpqrcode.php');
		$value = $url;//二维码内容 
		// $value = "/index/main/qrcode?device_id=".$id;//二维码内容 

		$errorCorrectionLevel = 'L';//容错级别 
		$matrixPointSize = 6;//生成图片大小 
		$cnname = $company['name'].'<br />'.$info['name'];
		// $name2 = iconv('UTF-8', 'GB18030', $name);
        // $name = $this->zone['id'].'_'.$this->info['id'];
        $name = $company['id'].'_'.$info['id'];

		$dir = "./upload/device/{$company['id']}/";
        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }
		
		//生成二维码图片 
		$file = $dir.$name.'.png';
		$code = $dir.$name.'_code.png';
		QRcode::png($value, $file, $errorCorrectionLevel, $matrixPointSize, 6); 

		if($company['logo']){
			$logo = './upload/'.$company['logo'];//准备好的logo图片 
		}else{
			$logo = './upload/logo.png';
		}
		$QR = $file;//已经生成的原始二维码图 

		if ($logo !== FALSE) { 
		 $QR = imagecreatefromstring(file_get_contents($QR)); 
		 $logo = imagecreatefromstring(file_get_contents($logo)); 
		 $QR_width = imagesx($QR);//二维码图片宽度 
		 $QR_height = imagesy($QR);//二维码图片高度 
		 $logo_width = imagesx($logo);//logo图片宽度 
		 $logo_height = imagesy($logo);//logo图片高度 
		 $logo_qr_width = $QR_width / 5; 
		 $scale = $logo_width/$logo_qr_width; 
		 $logo_qr_height = $logo_height/$scale; 
		 $from_width = ($QR_width - $logo_qr_width) / 2;
		 // $logo_qr_height = $logo_qr_height + 110; 
		 // dump($logo_qr_width);exit;
		 //重新组合图片并调整大小 
		 imagecopyresampled($QR, $logo, $from_width, $from_height, 0, 0, $logo_qr_width, 
		 $logo_qr_height, $logo_width, $logo_height); 
		} 


		$text = $company['name']."--".$info['name'];
		$this->mark_photo($file,$text,$file);


		$file = str_replace('./', '', $file);

		//在图片中添加文字拼接图片
		//输出图片 
		// Header("Content-type: image/png");
		// ImagePng($QR,$code);

		// echo $cnname.'<br />';
		echo '<img src="/'.$file.'"  alt="" />';
		
    }
    
    public function mark_photo($background,$text,$filename){
		
		$image = imagecreatefrompng($background);
		$font = './static/app/fonts/msyh.TTF';
		$color = imagecolorallocate($image,0,0,0);
		imagettftext($image, 10, 0, 60, 25, $color, $font, $text);
		// header("Content-Type:image/png");
		ImagePng($image, $filename);
		imagedestroy($image);
	}
}